<?php


use Plugins\Demo\Http\Admin\Controller\IndexController;
use Sc\Util\HtmlStructure\Form;
use Sc\Util\HtmlStructure\Form\FormItem;
use Sc\Util\HtmlStructure\Html\Html;
use Sc\Util\HtmlStructure\Html\Js;
use Sc\Util\HtmlStructure\Html\Js\VueComponents\Temporary;
use Sc\Util\HtmlStructure\Table;

Html::create('表格异步操作');

$table = Table::create(
    // 第一个参数为数据接口地址， sc_route函数为封装的路由函数，自动吧控制器的方法地址转为url
    sc_route([IndexController::class, 'demoListData']),
    // 第二个参数为表格ID，用于区分多个表格
    'demo3'
)->addColumns(
    Table\Column::selection(),
    Table\Column::normal('ID', 'id'),
    Table\Column::normal('姓名', 'name'),
    Table\Column::normal('年龄', 'age'),
    Table\Column::normal('状态', 'status'),
    Table\Column::normal('简介', 'intro'),
    Table\Column::normal('创建时间', 'created_at'),
);

$table->setHeaderEvent("@danger.delete.删除",
    Table\EventHandler::post(
        url: sc_route([IndexController::class, 'demoDel']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: Js::code("this.\$message.success('删除成功')")->then("this.demo3GetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);

// header 事件直接的异步请求默认会验证是否有选择数据，选择的数据变量为 selection
$table->setHeaderEvent("@primary.发起请求",
    Table\EventHandler::get(
        url: sc_route([IndexController::class, 'demoListData']),
        data: ['ids' => '@selection.map(v => v.id)'],
        successHandler: Js::code("this.\$message.success('删除成功')")->then("this.demo3GetData()")
    )->confirmMessage('确认删除选中数据吗？')
        ->addLoading()
);

$table->setHeaderEvent("@primary.带表单数据发起请求",
    Table\EventHandler::window('带表单数据发起请求')
        // 设置表单的数据，这里取的选择的数据的id值
        ->setRowData(['ids' => '@selection.map(v => v.id)'])
        ->setContent(
            Form::create("demo3Form")->config(['label-width' => '80px'])->addFormItems(
                FormItem::hidden( 'ids',),
                FormItem::text( 'name', '姓名',),
                FormItem::text( 'age', '年龄',),
                FormItem::radio( 'status', '状态',)->options([
                    ['value' => 1, 'label' => '正常'],
                    ['value' => 2, 'label' => '禁用'],
                ]),
                FormItem::textarea( 'intro', '简介',),
                FormItem::datetime( 'created_at', '创建时间',),
                FormItem::submit()->submitUrl(sc_route([IndexController::class, 'demoDel']))
            )->render()
        )
);

$table->setRowEvent("@primary.带表单数据发起请求",
    Table\EventHandler::window('带表单数据发起请求')
        // 设置表单的数据，这里取的行数据的id值
        ->setRowData(['id' => '@row.id'])
        ->setContent(
            Form::create("demo3Form")->config(['label-width' => '80px'])->addFormItems(
                FormItem::hidden( 'id',),
                FormItem::text( 'name', '姓名',),
                FormItem::text( 'age', '年龄',),
                FormItem::radio( 'status', '状态',)->options([
                    ['value' => 1, 'label' => '正常'],
                    ['value' => 2, 'label' => '禁用'],
                ]),
                FormItem::textarea( 'intro', '简介',),
                FormItem::datetime( 'created_at', '创建时间',),
                FormItem::submit()->submitUrl(sc_route([IndexController::class, 'demoDel']))
            )->render()
        )
);


return $table->render();